<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	$groups=pdo_getall('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid']),'','','paixu asc');
	foreach($groups as &$item){
		$item['shu']=pdo_count('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'classify_id'=>$item['id']));
	}
	$return['post']=array();
	$return['groups']=$groups;
	$id = intval($_RQ['id']);
	if(!$id){
		$id=$groups['0']['id'];
	}
	$return['params']=$params=array();
	$return['params']['id']=$id;
	$return['list']=$list;
	$sharelink='jy_newnong/gclassify//fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>'果树列表',
			'desc'=>$return['setting']['share_desc']?$return['setting']['share_desc']:'作物列表-天天农场，一个属于自己的农场！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($return['setting']['share_image'])
	);
	set_html('title','认养列表');
	exi($return);
}
if($_RQ['op']=='detail'){
	$id = intval($_RQ['id']);
	if(!$id){
		exi('没有找到该分类，请重新选择！','error','jy_newnong/gclassify/list');
	}
	//查询该分类下的果树
	$item=pdo_get('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['image']=tomedia($item['image']);
	$isnum=pdo_count('jy_farmer_gdopt',array('uniacid'=>$_SESSION['uniacid'],'guoshu_id'=>$item['id']));
	$item['kucun'] -=$isnum;
	$item['endtime']=date('Y-m-d',$item['endtime']);
	$item['videos']=json_decode($item['videos'],true);
	foreach($item['videos'] as &$vo){
		$vo['video']=tomedia($vo['video']);
		$vo['thumb']=tomedia($vo['thumb']);
	}
	$return['item']=$item;
	$return['post']=array();
	$sharelink='jy_newnong/gclassify/detail/id='.$item['id'].'&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$item['title'],
			'desc'=>$item['remark']?:'果树-天天农场，一个属于自己的农场！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($item['image'])
	);
	set_html('title',$item['title']);
	exi($return);
}

if($_RQ['op']=='animal_list'){
	$post=get_postdata();
	$id =$_RQ['id'];
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['classify_id']=$id;
	$params['status']=1;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','kucun','price','remark','growtime','classify_id','image','status','shoukg','shoufee','endtime');
	$animal_list=pdo_getslice('jy_farmer_guoshu',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$acalssify=pdo_get('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	foreach($animal_list as &$item){
		$item['endtime']=date('Y-m-d',$item['endtime']);
		$isnum=pdo_count('jy_farmer_gdopt',array('uniacid'=>$_SESSION['uniacid'],'guoshu_id'=>$item['id']));
		$item['kucun'] -=$isnum;
	}
	$return['list']=$animal_list;
	$return['acalssify']=$acalssify;
	$return['cid']=$id;
	exi($return);
}
if($_RQ['op']=='notice'){
	$post=get_postdata();
	$guoshu_id = $this->session('guoshu_id');
	if($post){
			$animal=pdo_get('jy_farmer_guoshu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$guoshu_id));
			$acalssify=pdo_get('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$animal['classify_id']));
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'guoshu_id'=>$this->session('guoshu_id'),
				'username'=>$post['username'],
				'mobile'=>$post['mobile'],
				'address'=>$post['address'],
				'time'=>$post['time'],
				'money'=>$animal['price'],
				'ordersn'=>get_ordersn('jy_farmer_order','ordersn'),
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_farmer_order',$insert);
			$id=pdo_insertid();
	}
	exi('','','jy_newnong/book/main/id='.$id);
}